const qs = require('qs')

//  import { registerFace } from '../interface/register'
interface registerFace {
    url: string,
    type: string,
    body: string
}
interface listFace {
    username: string,
    password: string,
    email: string,
    level: string
}
const registerInterface = (req: registerFace) => {
    const params = JSON.parse(req.body)
    console.log('注册账号 传递参数', params)

    // 获取本地数据 用以判断以何种方式进行写入
    const registerList: any = localStorage.getItem('register_list') || []

    let list: listFace[] = []
    console.log('registerList || registerList.length !== 0', registerList, registerList.length !== 0)
    if (registerList && registerList.length !== 0) {
        list = JSON.parse(registerList)
    }

    let code: number = 401
    let msg: string = '账号已存在'

    let bool = list.findIndex(item => item.email === params.email)

    if (bool < 0) {
        code = 0
        msg = '注册成功'
        list.push({
            username: params.username,
            email: params.email,
            password: params.password,
            level: params.level
        })
        // 写入本地（即进行注册操作）
        localStorage.setItem('register_list', JSON.stringify(list))
    }

    return { code, msg }
}


export default registerInterface